package starcoast.bocai.com.starcoast.activitys.life;

import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

import starcoast.bocai.com.starcoast.R;
import starcoast.bocai.com.starcoast.adapter.SearchBusinessAdapter;
import starcoast.bocai.com.starcoast.common.BaseActivity;
import starcoast.bocai.com.starcoast.view.ExpandTabView;
import starcoast.bocai.com.starcoast.view.MyListView;
import starcoast.bocai.com.starcoast.view.ViewLeft;
import starcoast.bocai.com.starcoast.view.ViewRight;

/**
 * Created by admin on 2016/6/15.
 */
public class SearchBusinessActivity extends BaseActivity {
    private ExpandTabView expandTabView;
    private ArrayList<View> mViewArray = new ArrayList<View>();
    private String[] items = new String[] { "一期", "二期"};//显示字段
    private ViewLeft viewLeft;
    private ViewRight viewRight;
    private SearchBusinessAdapter adapter;
    private ListView lv_business;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_business);
        initView();
        initVaule();
        initListener();
    }
    private void initView() {
        expandTabView = (ExpandTabView) findViewById(R.id.expandtab_view);
        lv_business = (ListView) findViewById(R.id.lv_business);
        viewLeft = new ViewLeft(this);
        viewRight = new ViewRight(this,items);
        adapter = new SearchBusinessAdapter(this);
        initToolbar("商家列表");
        initToolBack();
        lv_business.setAdapter(adapter);
    }
    private void initVaule() {

        mViewArray.add(viewLeft);
        mViewArray.add(viewRight);
        ArrayList<String> mTextArray = new ArrayList<String>();
        mTextArray.add("全部");
        mTextArray.add("距离");
        expandTabView.setValue(mTextArray, mViewArray);
//        expandTabView.setTitle(viewLeft.getShowText(), 0);
//        expandTabView.setTitle(viewRight.getShowText(), 1);

    }

    private void initListener() {

        viewLeft.setOnSelectListener(new ViewLeft.OnSelectListener() {

            @Override
            public void getValue(String distance, String showText) {
                onRefresh(viewLeft, showText);
            }
        });

        viewRight.setOnSelectListener(new ViewRight.OnSelectListener() {

            @Override
            public void getValue(String distance, String showText) {
                onRefresh(viewRight, showText);
            }
        });

    }

    private void onRefresh(View view, String showText) {

        expandTabView.onPressBack();
        int position = getPositon(view);
        if (position >= 0 && !expandTabView.getTitle(position).equals(showText)) {
//            expandTabView.setTitle(showText, position);
        }
        Toast.makeText(SearchBusinessActivity.this, showText, Toast.LENGTH_SHORT).show();

    }

    private int getPositon(View tView) {
        for (int i = 0; i < mViewArray.size(); i++) {
            if (mViewArray.get(i) == tView) {
                return i;
            }
        }
        return -1;
    }

    @Override
    public void onBackPressed() {

        if (!expandTabView.onPressBack()) {
            finish();
        }

    }
}
